package org.skydiveapp;

import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.Constants;
import org.skydiveapplibrary.repository.SettingsRepository;
import org.skydiveapplibrary.ui.ImageManager;
import org.skydiveapplibrary.util.FileManager;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class LogEntryPhotoActivity extends SherlockActivity
{	
	// data
	private boolean isDualDisplay;
	private String filename;
	
	private OnClickListener openPhotoExternally = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			FileManager fileManager = new FileManager(LogEntryPhotoActivity.this);
			if (fileManager.getUseExternalStorage())
			{
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_VIEW);
				intent.setDataAndType(Uri.fromFile(fileManager.getStorageFile(filename)), "image/*");
				startActivity(intent);
			}
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		SettingsRepository settingsRepository = new SettingsRepository(this);
		if (settingsRepository.getUseLightTheme())
			super.setTheme(R.style.SkydiveAppTheme_Light_SemiTransparent);
		
		super.onCreate(savedInstanceState);

		setContentView(R.layout.log_entry_photo_view);

		isDualDisplay = this.findViewById(R.id.phoneLayout) == null;
		
		// get image view
		ImageView imageView = (ImageView) findViewById(R.id.LogEntryPhotoImageView);
		// load data
		filename = getIntent().getStringExtra(Constants.EXTRA_PHOTO);
		if (filename != null)
		{
			ImageManager imageManager = new ImageManager(this);
			Bitmap bitmap = imageManager.decodeToScreenSize(filename);
			imageView.setImageBitmap(bitmap);
			imageView.setOnClickListener(openPhotoExternally);
		}
	}
	
	private void delete()
	{
		// set result intent
		Intent intent = new Intent();
		intent.putExtra(Constants.EXTRA_PHOTO_DELETE, true);
		setResult(RESULT_OK, intent);
		
		// close
		close();
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	MenuInflater inflater = getSupportMenuInflater();
    	inflater.inflate(R.menu.menu_open_log_entry, menu);
    	inflater.inflate(R.menu.menu_delete, menu);
    	inflater.inflate(R.menu.menu_back, menu);
    	return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
    	menu.findItem(R.id.menu_back).setVisible(isDualDisplay);
    	menu.findItem(R.id.menu_open_log_entry)
    		.setVisible(getIntent().getBooleanExtra(Constants.EXTRA_GOTO_LOG_ENTRY, false));

    	return true;
    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Handle item selection
       int itemId = item.getItemId();
       if (itemId == R.id.menu_open_log_entry) {
			// set result intent
			Intent intent = new Intent();
			intent.putExtra(Constants.EXTRA_GOTO_LOG_ENTRY, true);
			setResult(RESULT_OK, intent);
			
			// close
			close();
			return true;
		}
		else if (itemId == R.id.menu_delete)
		{
			delete();
			return true;
		}
		else if (itemId == R.id.menu_back)
		{
			close();
			return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onBackPressed()
	{
		close();
	}

	private void close()
	{
		finish();
	}
}
